package org.qloudgen.borg.pages.panel;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.FuncBean;
import org.qloudgen.borg.beans.ResultBean;
import org.qloudgen.borg.pages.model.FuncResultModel;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class AppFuncResultPanel extends Panel {
        
	private static final long serialVersionUID = 1L;
	private transient FuncResultModel fpm;
	
    public AppFuncResultPanel(String id , final FuncBean func)
    {
        super(id);
		
		Form<?> form = new Form("form",new CompoundPropertyModel<FuncResultModel>(new FuncResultModel())) {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				
				fpm = (FuncResultModel)getDefaultModelObject();
					
				if (fpm.getGroup() == null){
					
					info(CommonValues.INFO_NO_SELECT);
					return;
					
				} 
			}
		};
		this.add(form);
		
		final RadioGroup<?> group=new RadioGroup<Object>("group"); 
		group.setOutputMarkupId(true);
		group.setRenderBodyOnly(false);
		form.add(group);
		
		final PageableListView<ResultBean> listView =new PageableListView<ResultBean>( "rows", func.getColumns(), CommonValues.SHOW_LIST_NUMBER) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<ResultBean> item) {
				final ResultBean column = (ResultBean) item.getModelObject();
				item.add(new Radio<ResultBean>("radio", new Model<ResultBean>(column)));
				item.add( new Label( "columnCode", column.getColumnCode()));
				item.add( new Label( "columnName", column.getColumnName()));
				item.add( new Label( "isOrder", CommonValues.isOrders.get(column.getIsOrder())));				
				
			}
		};
		listView.setOutputMarkupId(true);
		group.add(listView);
		
        // window
		final ModalWindow modal = new ModalWindow("modal");
        this.add(modal);

        modal.setContent(new AddFuncResultPanel(modal.getContentId(), func , modal));
        modal.setTitle("添加结果表字段");
        modal.setCookieName("modal");

        modal.setCloseButtonCallback(new ModalWindow.CloseButtonCallback()
        {
			private static final long serialVersionUID = 1L;

			@Override
            public boolean onCloseButtonClicked(AjaxRequestTarget target)
            {
                return true;
            }
        });

        modal.setWindowClosedCallback(new ModalWindow.WindowClosedCallback()
        {
			private static final long serialVersionUID = 1L;

			@Override
            public void onClose(AjaxRequestTarget target)
            {
                target.add(group);
            }
        });

		// AjaxLink add 
		form.add(new AjaxLink<Void>("add"){

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				
				modal.show(target);
			}});
		
		// AjaxButton delete
		form.add(new AjaxSubmitLink("delete"){
			
			private static final long serialVersionUID = 1L;
			protected void onAfterSubmit(AjaxRequestTarget target, Form<?> form) {
				
				for (int i = 0; i<func.getColumns().size(); i++) {
					if (func.getColumns().get(i).getColumnCode().equals(fpm.getGroup().getColumnCode())){
						func.getColumns().remove(i);
						break;
					}
				}
				target.add(group);
				
			}});
    }

}
